package com.fansl.allround.common.security.component;

import com.fansl.allround.common.core.constant.CommonConstants;
import com.fansl.allround.common.security.exception.AllroundAuth2Exception;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/**
 * @author fansl
 * @Description: OAuth2 异常格式化
 * @date 2019/7/12 10:56
 */
public class AllroundAuth2ExceptionSerializer extends StdSerializer<AllroundAuth2Exception> {
    public AllroundAuth2ExceptionSerializer() {
        super(AllroundAuth2Exception.class);
    }

    @Override
    public void serialize(AllroundAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeObjectField("code", CommonConstants.FAIL);
        jsonGenerator.writeStringField("msg", e.getMessage());
        jsonGenerator.writeStringField("data", e.getErrorCode());
        jsonGenerator.writeEndObject();
    }
}
